using UnityEngine;
using System.Collections;

public class tankMove : MonoBehaviour {

	// Use this for initialization
	public GameObject terrain;
	Vector3 movimento;
	Vector3 rotacao;
	float speed;
	public float maxSpeed = 20;
	public float grav;
	
	void Start () {
		grav = -400;
	}
	
	// Update is called once per frame
	// Falta limitar a rotação em z e x para o carro não capotar
	void Update () {
		if (Input.GetAxis("Vertical")!= 0){
//			speed++;
//			if (speed > maxSpeed){
//				speed= maxSpeed;
//			}
//		}else{
//			speed--;
//			if (speed < 0){
//				speed = 0;
//			}
			speed = 200;
		}
		transform.Rotate(0, Input.GetAxis("Horizontal")* rigidbody.velocity.magnitude/40, 0);
		movimento = (Input.GetAxis("Vertical")* speed * transform.forward);
		
		rigidbody.AddForce(movimento, ForceMode.Acceleration);
		rigidbody.AddForce (new Vector3(0,grav,0), ForceMode.Acceleration);
		
		if (transform.rotation.z > 90 && transform.rotation.z < 270){
			transform.rotation = new Quaternion(0,0,0,0);
		}
		
		
	}
	
	void OnCollisionEnter (Collision collision){
		if (collision.gameObject.tag == "Terreno"){
			grav = 0;
		}
	}
	
	void OnCollisionStay (Collision collision){
		if (collision.gameObject.tag == "Terreno"){
			grav = 0;
		}
	}
	
	void OnCollisionExit (Collision collision){
		if (collision.gameObject.tag == "Terreno"){
			grav = -400;
		}
	}
	
	
}
